<?php
/**
 * 系統名稱: Lotto 系統
 * 檔案說明: 專案管理-修改
 * $Author$
 * $Id$
 *
 */
if (!defined("WagerList_lib_include")){
	define("WagerList_lib_include", 1);
	include_once(__Modules_Path."/ChangeTime.lib.php");
	include_once(__Modules_Path."/WagerForm.lib.php");
/**
 * 取得下注明細 XML 型態
 * @param	intGID 操盤序號
 * @param	strBT 下注種類
 * @param	intNum 號碼
 * @param	intUserID 使用者序號
 * @param	intUserID 父層序號
 * @param	strPower 權限種類
 * @param	isSto 是否已開彩
 * @param	isRealShow 是否顯示實貨
 * @param	isParlays 是否過關
 * @param	strWagerType 注單種類
 * @return	String
 */
	function getWagerList_XMLform($intGID, $strBT, $intNum, $intUserID, $intParentID, $strPower, $isStop, $isRealShow, $isParlays, $strWagerType){
		global $_Conn, $$_Conn, $_Charset;
		if($isParlays >= 1){
			$arrBetID = array(0);
			if($intNum == -1){
				$strSQL = "select BetListID from `bet_ParlaysNumber` where GambleID = '".$intGID."' and Bet_Type in (".$strBT.") ";
				if($isParlays == 2)$strSQL = "select BetListID from `bet_ParlaysNumberEven` where GambleID = '".$intGID."' and Bet_Type in (".$strBT.") ";
			}else{
				$strSQL = "select BetListID from `bet_ParlaysNumber` where GambleID = '".$intGID."' and Bet_Type in (".$strBT.") and Number = '".$intNum."'";
				if($isParlays == 2)$strSQL = "select BetListID from `bet_ParlaysNumberEven` where GambleID = '".$intGID."' and Bet_Type in (".$strBT.") and (`Ball1` = '".$intNum."' or `Ball2` = '".$intNum."' or `Ball3` = '".$intNum."' or `Ball4` = '".$intNum."' or `Ball5` = '".$intNum."')";
			}

			//print $strSQL;
			if($RS = @PMA_mysql_query($strSQL, $$_Conn)){
				while($Rows = PMA_mysql_fetch_object($RS)){
					$arrBetID[] = $Rows -> BetListID;
				}
			}
		}

		$strSubmitForm = "BetForm";
		$strN = "select ID, LottoID, MemCommission as MC, Bet_Type, MemStake*ROUND(MemExchangeRate, 4){UserRate} as MemStake, MemStake as AllMemStake, SubIncome*ROUND(MemExchangeRate, 4){UserRate} as SubIncome, WagerType ";

		switch($strPower){
			case "all":					//顯示全部及時注單
				$strSeach = "";
				$strN = str_replace("*ROUND(MemExchangeRate, 4){UserRate}", ($isRealShow == 1)?"*IF(WagerType = 1,1,ROUND(MemExchangeRate, 4)*ROUND(DirectorRate, 4))":"", $strN);
				break;

			case "sub":					//顯示該代理商全部及時注單
				$strSeach = " and SubID = ".$intUserID." ";
				$strN = str_replace("{UserRate}", ($isRealShow == 1 && !ereg("-1", $strWagerType))?"*IF(WagerType = 2,1,(1 - SubRate))":"", $strN);
				break;

			case "main":				//顯示該總代理全部及時注單
				$strSeach = " and MainID = ".$intUserID." ";
				$strN = str_replace("{UserRate}", ($isRealShow == 1 && !ereg("-1", $strWagerType))?"*IF(WagerType = 3,1,(SubRate - MainRate))":"", $strN);
				break;

			case "stock":				//顯示該股東全部及時注單
				if($isRealShow == 2 && $_SESSION["Admin_Info"] -> isAllAccount == 1){
					if($_SESSION["Admin_Info"] -> ParentisAllAccount == 1){
						$strSeach = " ";
					}else{
						$strSeach = " and DirectorID = ".$intParentID." ";
					}
					$strN = str_replace("{UserRate}", "*IF(WagerType = 4,1,DirectorRate)", $strN);
				}else{
					$strSeach = " and StockID = ".$intUserID." ";
					$strN = str_replace("{UserRate}", ($isRealShow == 1 && !ereg("-1", $strWagerType))?"*IF(WagerType = 4,1,(MainRate - StockRate))":"", $strN);
				}
				break;

			case "director":				//顯示該總監全部及時注單
				if($isRealShow == 2){
					$strSeach = " ";
					$strN = str_replace("{UserRate}", "*IF(WagerType = 5,1,DirectorRate)", $strN);
				}else{
					$strSeach = " and DirectorID = ".$intUserID." ";
					$strN = str_replace("{UserRate}", ($isRealShow == 1 && !preg_match("/-1/", $strWagerType))?"*IF(WagerType = 5,1,(StockRate - DirectorRate))":"", $strN);
				}
				break;
		}

		$strTable = ($isStop == 1)?"bet_BetList":"bet_BetListTmp";
		$strNum = ($intNum == -1)?"":"and Number = ".$intNum;
		if($isParlays >= 1){
			$strSQL = $strN." from `".$strTable."` where ID in (".join(", ", $arrBetID).") and isPayOff = 0 and WagerType in (".$strWagerType.") ".$strSeach;
		}else{
			$strSQL = $strN." from `".$strTable."` where Bet_Type in (".$strBT.") and GambleID = ".$intGID." ".$strNum." and isPayOff = 0 and WagerType in (".$strWagerType.") ".$strSeach;
		}
		//print $strSQL;
		if($RS = @PMA_mysql_query($strSQL, $$_Conn)){
			while($Rows = PMA_mysql_fetch_object($RS)){
				$arrRows[$Rows -> ID] = $Rows;
				$arrID[] = $Rows -> ID;
			}
		}

		$arrOneStake[1] = array(7, 8, 9, 10, 11, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43);
		$arrOneStake[2] = array();
		$arrOneStake[3] = array();
		$arrOneStake[4] = array();
		$arrOneStake[5] = array(15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 30, 31, 32, 33, 34, 35);

		$strCon = "<?xml version=\"1.0\" encoding=\"".$_Charset."\"?".">";
		$strCon.= "<root>";
		if(is_array($arrRows)){
			if(sizeof($arrRows) > 0){
				$strSQL = "select ID, BetDate as BD, BetTime as BT, Content, MemAccount, OneStake from`bet_BetListContent` where ID in (".join(",", $arrID).")";
				if($RS = @PMA_mysql_query($strSQL, $$_Conn)){
					while($Rows = PMA_mysql_fetch_object($RS)){
						$arrConRows[$Rows -> ID] = $Rows;
					}
				}
				while(list($k, $Rows)=each($arrRows)){
					$strCon.= "<WagerData>";
					$strCon.= "\t<BetDate>".DateForm($arrConRows[$k] -> BD, $arrConRows[$k] -> BT)."</BetDate>\n";
					$strCon.= "\t<BetTime>".TimeForm($arrConRows[$k] -> BD, $arrConRows[$k] -> BT)."</BetTime>\n";
					$strCon.= "\t<BetNo>".$Rows -> ID."</BetNo>\n";
					$strCon.= "\t<Account>".$arrConRows[$k] -> MemAccount." ".(100 - $Rows -> MC*100)."</Account>\n";
					$strTmp = WagerFormTrans($Rows -> LottoID, $Rows -> Bet_Type, $arrConRows[$k] -> Content);
					$strTmp = htmlspecialchars($strTmp);
					$strCon.= "\t<Content>".$strTmp."</Content>\n";
					$strCon.= "\t<Stake>".sprintf("%.1f",$Rows -> MemStake)."</Stake>\n";
					$strCon.= "\t<OneStake>";
					//if($Rows -> Bet_Type >= 7 && $Rows -> Bet_Type <= 11 && !empty($arrConRows[$k] -> OneStake)){
					if(is_array($arrOneStake[$Rows -> LottoID]) && in_array($Rows -> Bet_Type, $arrOneStake[$Rows -> LottoID]) && !empty($arrConRows[$k] -> OneStake)){
						if(round($Rows -> AllMemStake / $arrConRows[$k] -> OneStake) > 1){
							$strCon.= round($Rows -> AllMemStake / $arrConRows[$k] -> OneStake)." x ".sprintf("%.1f", ($Rows -> MemStake / $Rows -> AllMemStake)*$arrConRows[$k] -> OneStake);
						}
					}
					$strCon.= "</OneStake>\n";
					$strCon.= "\t<Income>".sprintf("%.1f",$Rows -> SubIncome)."</Income>\n";
					$strCon.= "\t<WType>".$Rows -> WagerType."</WType>\n";
					$strCon.= "</WagerData>\n";
				}
			}
		}
		$strCon.= "</root>\n";
		return $strCon;
	}

/**
 * 取得已開獎下注統計表 XML 型態
 * @param	intGID 操盤序號
 * @param	intUserID 使用者序號
 * @param	strPower 權限種類
 * @param	isStop 是否已開彩
 * @return	 String
 */
	function getWagerStartGameList_XMLform($intGID, $intUserID, $strPower, $isStop){
		global $_Conn, $$_Conn, $_arrBet_Type;
		
		$strSubmitForm = "BetForm";
		$strN = "select Bet_Type, Count(*) as total, SUM(MemStake*ROUND(MemExchangeRate, 4)) as MemStake, SUM(MemStake*ROUND(MemExchangeRate, 4){UserRate}) as MemStake1 ";
		switch($strPower){
			case "all":						//顯示全部及時注單
				$strSeach = "";
				$strN = "select Bet_Type, Count(*) as total, SUM(MemStake) as MemStake, SUM(MemStake*ROUND(MemExchangeRate, 4)*ROUND(DirectorRate, 4)) as MemStake1 ";
				$strN = str_replace("{UserRate}", "*DirectorRate", $strN);
				break;

			case "sub":						//顯示該代理商全部及時注單
				$strSeach = " and SubID = ".$intUserID." ";
				$strN = str_replace("{UserRate}", "*(1 - SubRate)", $strN);
				break;

			case "main":					//顯示該總代理全部及時注單
				$strSeach = " and MainID = ".$intUserID." ";
				$strN = str_replace("{UserRate}", "*(SubRate - MainRate)", $strN);
				break;

			case "stock":					//顯示該股東全部及時注單
				$strSeach = " and StockID = ".$intUserID." ";
				$strN = str_replace("{UserRate}", "*(MainRate - StockRate)", $strN);
				break;

			case "director":				//顯示該總監全部及時注單
				$strSeach = " and DirectorID = ".$intUserID." ";
				$strN = str_replace("{UserRate}", "*(StockRate - DirectorRate)", $strN);
				break;
		}

		$strTable = ($isStop == 1)?"bet_BetList":"bet_BetListTmp";
		$strSQL = $strN." from `".$strTable."` where GambleID = ".$intGID." and isPayOff = 0 ".$strSeach." group by Bet_Type";// and WagerType = 0

		$arrRows = array();
		//print $strSQL;
		if($RS = @PMA_mysql_query($strSQL, $$_Conn)){
			while($Rows = PMA_mysql_fetch_object($RS)){
				if($Rows -> total == 0)continue;
				$arrRows[$Rows -> Bet_Type] = $Rows;
			}
		}
		$strCon = "<?xml version=\"1.0\" encoding=\"{Charset}\"?".">";
		$strCon.= "<root>";
		//for($i = 1; $i < sizeof($_arrBet_Type); $i++){
		foreach($arrRows as $intBT => $Rows){
			//$Rows = $arrRows[$i];
			$strCon.= "<WagerData>";
			$strCon.= "\t<GambleID>".$intGID."</GambleID>\n";
			$strCon.= "\t<GameDate>{GameDate}</GameDate>\n";
			$strCon.= "\t<GameTime>{GameTime}</GameTime>\n";
			$strCon.= "\t<GameNo>{GameNo}</GameNo>\n";
			$strCon.= "\t<BID>".$intBT."</BID>\n";
			$strCon.= "\t<BetType>{B_".$intBT."}</BetType>\n";
			$strCon.= "\t<CountNo>".$Rows -> total."</CountNo>\n";
			$strCon.= "\t<AllMemStake>".round($Rows -> MemStake, 1)."</AllMemStake>\n";
			$strCon.= "\t<RateMemStake>".round($Rows -> MemStake1, 1)."</RateMemStake>\n";
			$strCon.= "</WagerData>\n";
		}
		$strCon.= "</root>\n";
		return $strCon;
	}


/**
 * 取得連碼補貨明細 XML 型態
 * @param	intGID 操盤序號
 * @param	intBID 下注種類
 * @param	intNumber 號碼
 * @param	isRealShow 是否顯示實貨
 * @param	intBValue 切牌金額
 * @param	intBOdds 切牌賠率
 * @param	intUserID 使用者序號
 * @param	strPower 權限種類
 * @return	String
 */
	function getSuppleJWList_XMLform($intGID, $intBID, $intNumber, $isRealShow, $intBValue, $intBOdds, $intUserID = 0, $strPower = ""){
		global $_Conn, $$_Conn, $_Charset;

		//$strSQL = "select a.Number as Number, SUM(a.MemPayOff{UserRate}) as MemPayOff, SUM(a.MemPayOff2{UserRate}) as MemPayOff2, COUNT(a.ID) as WagerCount from `bet_ParlaysNumberEven` a, `bet_BetListTmp` b where a.GambleID = ".$intGID." and a.isPayOff = 0 and a.Bet_Type = ".$intBID." and a.BetListID = b.ID group by Number having MemPayOff > ".(string)round($intBValue*-1)." order by MemPayOff desc";
		if($intBID == 30){
			$strSQL = "select a.JoinNumber as JoinNumber, b.Number as Number, SUM(b.MemPayOff*ROUND(b.MemExchangeRate, 4){UserRate}) as MemPayOff, COUNT(a.ID) as WagerCount from `bet_BetListContent` a, `bet_BetListTmp` b where b.GambleID = ".$intGID." and b.isPayOff = 0 and b.Bet_Type = ".$intBID." and a.ID = b.ID and b.Number = ".$intNumber;
			$strGroupBy = "a.JoinNumber";

		}else{
			$strSQL = "select a.Number as Number, SUM(a.MemStake*a.Odds1*ROUND(b.MemExchangeRate, 4){UserRate}) as MemPayOff, SUM(a.MemStake*a.Odds2*ROUND(b.MemExchangeRate, 4){UserRate}) as MemPayOff2, COUNT(a.ID) as WagerCount from `bet_ParlaysNumberEven` a, `bet_BetListTmp` b where a.GambleID = ".$intGID." and a.isPayOff = 0 and a.Bet_Type = ".$intBID." and a.BetListID = b.ID";
			$strGroupBy = "a.Number";
		}
		switch($strPower){
			case "sub":						//顯示該代理商全部及時注單
				$strSeach = " and b.SubID = ".$intUserID." ";
				$strSQL = str_replace("{UserRate}", ($isRealShow == 1)?"*IF(b.WagerType = 2,-1,1 - b.SubRate)":"", $strSQL);
				break;

			case "main":					//顯示該總代理全部及時注單
				$strSeach = " and b.MainID = ".$intUserID." ";
				$strSQL = str_replace("{UserRate}", ($isRealShow == 1)?"*IF(b.WagerType = 3,-1,b.SubRate - b.MainRate)":"", $strSQL);
				break;

			case "stock":					//顯示該股東全部及時注單
				$strSeach = " and b.StockID = ".$intUserID." ";
				$strSQL = str_replace("{UserRate}", ($isRealShow == 1)?"*IF(b.WagerType = 4,-1,b.MainRate - b.StockRate)":"", $strSQL);
				break;

			case "director":				//顯示該總監全部及時注單
				$strSeach = " and b.DirectorID = ".$intUserID." ";
				$strSQL = str_replace("{UserRate}", ($isRealShow == 1)?"*IF(b.WagerType = 5,-1,b.StockRate - b.DirectorRate)":"", $strSQL);
				break;

			default:
				$strSQL = str_replace("{UserRate}", ($isRealShow == 1)?"*ROUND(b.DirectorRate, 4)":"", $strSQL);
				break;
		}

		$strSQL = $strSQL.$strSeach." group by ".$strGroupBy." having MemPayOff > ".(string)round($intBValue*-1)." order by MemPayOff desc";

		//print $strSQL;
		$RS = @PMA_mysql_query($strSQL, $$_Conn);

		$strCon = "<?xml version=\"1.0\" encoding=\"".$_Charset."\"?".">";
		$strCon.= "<root>";
		while($Rows = PMA_mysql_fetch_object($RS)){
			$strCon.= "<WagerData>";
			$strCon.= "\t<Number>".$Rows -> Number."</Number>\n";
			$strCon.= "\t<JoinNumber>".$Rows -> JoinNumber."</JoinNumber>\n";
			$strCon.= "\t<MemPayOff>".round($Rows -> MemPayOff)."</MemPayOff>\n";
			$strCon.= "\t<MemPayOff2>".round($Rows -> MemPayOff2)."</MemPayOff2>\n";
			$strCon.= "\t<WagerCount>".round($Rows -> WagerCount)."</WagerCount>\n";
			$strCon.= "</WagerData>\n";
		}
		$strCon.= "</root>\n";
		return $strCon;
	}
//-------------------------------------------------------//
//WagerList_lib End
}
?>